<template>
	<view id="subSearchContainer">
		<!-- 搜索框 -->
		<view class="searchItem">
				<uni-search-bar @input="input" radius="100" cancelButton='none' placeholder="请输入搜索内容"></uni-search-bar>
		</view>
		<!-- 搜索建议 -->
		<view v-if="searchResult.length !== 0" class="searchResultBox">
			<view @click="gotoGoodsDeatil(item)" v-for="(item,index) in searchResult" :key="item.goods_id" class="searchResultItem">
				<view class="searchTitle">{{item.goods_name}}</view>
				<uni-icons type="arrow-right" size="16"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史 -->
		<view  class="searchHistoryBox">
			<view class="searchHistoryTitleBox">
				<text>搜索历史</text>
				<uni-icons @click="clrHistoryList" type="trash" size="16"></uni-icons>
			</view>
			<view class="searchHistoryItemBox">
				<uni-tag @click="switchItemDetail(item)" v-for="(item,index) in historyList" :key="index" :text="item" class="searchHistoryItem"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 防抖
				timer:null,
				// 搜索关键词
				key:"",
				// 搜索建议
				searchResult:[],
				// 历史搜索
				historyList:[]
			}
		},
		methods: {
			// 清除上一个定时器
			input(e){
				clearTimeout(this.timer)
				this.timer = setTimeout(()=>{
					this.key = e
					this.getSearchList()
				},500)
			},
			// 获得搜索建议
			async getSearchList(){
				if(this.key === ""){
					this.searchResult = []
					return
				}
				const {data:res} = await uni.$http.get("/api/public/v1/goods/qsearch",{query:this.key})
				if(res.meta.status === 200){
					this.searchResult = res.message
					this.getSearchHistoryList()
				}else{
					return uni.$showMsg()
				}
			},
			// 跳转商品详情
			 gotoGoodsDeatil(item){
				 uni.navigateTo({
				 	url:"/subpkg/goods_detail/goods_detail?goods_id="+item.goods_id
				 })
			},
			// 将搜索关键词存入搜索历史
			getSearchHistoryList(){
				this.historyList.unshift(this.key)
				// 去重
				this.historyList = Array.from(new Set(this.historyList))
				// 将搜索历史记录持久化保存到本地
				uni.setStorageSync('key',JSON.stringify(this.historyList))
			},
			// 清空搜索历史
			clrHistoryList(){
				this.historyList = []
				uni.removeStorageSync('key')
			},
			// 点击搜索历史跳转
			switchItemDetail(item){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query='+item
				})
			}
		},
		onLoad() {
			// 加载本地搜索历史
			this.historyList = JSON.parse(uni.getStorageSync('key') || "[]")
		}
	}
</script>

<style lang="scss">
.searchItem{
	position: sticky;
	top: 0;
	z-index: 999;
}
.searchResultBox{
	padding: 0 5px;
	.searchResultItem{
		font-size: 12px;
		padding: 13px 0;
		border-bottom: 1px solid #efefef;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.searchTitle{
			// 文字不换行
			white-space: nowrap;
			// 超出部分隐藏
			overflow: hidden;
			// 文本溢出后使用。。。代替
			text-overflow: ellipsis;
			margin-right: 3px;
		}
	}
}

.searchHistoryBox{
	padding: 0px 5px;
	.searchHistoryTitleBox{
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 40px;
		font-size: 13px;
		border-bottom: 1px solid #efefef;
	}
	.searchHistoryItemBox{
		display: flex;
		flex-wrap: wrap;
		.searchHistoryItem{
			margin-top: 5px;
			margin-right: 5px;
		}
	}
	
}

</style>
